import { message } from 'antd';
import { queryCurrent, editPassword } from '../services/user';

export default {
  namespace: 'user',

  state: {
    currentUser: {},
  },

  effects: {
    *fetchCurrent(_, { call, put }) {
      const payload = {userno: localStorage.getItem('userno')};
      const response = yield call(queryCurrent, payload);
      if(response.status){
        response.data.name = response.data.sname || response.data.tname;
        yield put({
          type: 'saveCurrentUser',
          payload: response.data,
        });
      }
    },
    *updatePassword({ payload }, { call }) {
      const response = yield call(editPassword, payload);
      if(response.status){
        message.success('修改成功')
      }
    },
  },

  reducers: {
    saveCurrentUser(state, action) {
      return {
        ...state,
        currentUser: action.payload,
      };
    },
    changeNotifyCount(state, action) {
      return {
        ...state,
        currentUser: {
          ...state.currentUser,
          notifyCount: action.payload,
        },
      };
    },
  },
};
